//
//  NPNavSearchView.m
//  newProject
//
//  Created by Mac mini on 2017/10/13.
//  Copyright © 2017年 zt. All rights reserved.
//

#import "NPNavSearchView.h"
#import "AppDelegate.h"

@implementation NPNavSearchView
{
    UIButton *searchButton;
}
-(instancetype)initRightButtonWithTitle:(NSString *)title withButtonImage:(NSString *)imageName withController:(UIViewController *)controller{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SearchNotification:) name:@"SearchNotify" object:nil];

    CGRect frame = CGRectMake(0, acestatusBarHeight, ACEWidth, onlyNavHeight);
    if (self) {
        
        if (@available(iOS 11.0,*)) {
            self = [super initWithFrame:frame];
        }else{
            self = [super initWithFrame:CGRectMake(50, acestatusBarHeight, ACEWidth-100, onlyNavHeight)];
        }
        
        self = [super initWithFrame:frame];
        self.backgroundColor = [UIColor clearColor];
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, onlyNavHeight-1, ACEWidth, 1)];
        line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:line];
        
        _controller = controller;
        _isSelect = NO;
       _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(15, 4, self.frame.size.width-70, 30)];
        _searchBar.returnKeyType = UIReturnKeySearch;
        _searchBar.placeholder = @"请输入要搜索的商品";
        _searchBar.searchBarStyle = UISearchBarStyleMinimal;
        _searchBar.delegate = self;
        [self addSubview:_searchBar];
        
        searchButton = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-60, 10, 40, 20)];
        searchButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [searchButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:searchButton];
        
        if (title) {
            _title = title;
            [searchButton setTitle:title forState:UIControlStateNormal];
            [searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }else if (imageName){
            _imageName = imageName;
            [searchButton setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
            [searchButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        }else{
            _searchBar.frame = CGRectMake(0, 4, frame.size.width, 30);
        }
        
    }
    return  self;
}

-(void)setRightTitle{
    _isSelect = YES;
    
    if (!searchButton) {
        _searchBar.frame = CGRectMake(0, 4, self.frame.size.width-40, 30);
         searchButton = [[UIButton alloc]initWithFrame:CGRectMake(ACEWidth-40, 10, 30, 20)];
//         searchButton = [[UIButton alloc]initWithFrame:CGRectMake(ACEWidth-40, 10, 30, 20)];
        [self addSubview:searchButton];
    }
    
    [searchButton setImage:nil forState:UIControlStateNormal];
    [searchButton setTitle:_title ? _title :@"取消" forState:UIControlStateNormal];
}

-(void)setRightImage{
    _isSelect = NO;
    [searchButton setTitle:nil forState:UIControlStateNormal];
    if (_imageName) {
        [searchButton setImage:[UIImage imageNamed:_imageName] forState:UIControlStateNormal];
    }else{
        [searchButton setImage:nil forState:UIControlStateNormal];
         _searchBar.frame = CGRectMake(0, 4, self.frame.size.width, 30);
    }
}

-(void)click{
//    if (_isSelect == YES) {
//        [self setRightImage];
        [self.searchBar resignFirstResponder];
//        [self hiddenSearchMaskView];
//    }else{
//        NSLog(@"消息状态");
//        if (self.delegate && [self.delegate respondsToSelector:@selector(showPushMessage)]) {
//            [self.delegate showPushMessage];
//        }
//    }
//    if (self.searchButtonCallBack) {
//        self.searchButtonCallBack(_isSelect);
//    }
}

//-(void)showSearchMaskView{
//    if (_maskView) {
//        _maskView.hidden = NO;
//
//    }else{
//        _maskView = [[NPSearchMaskView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, ACEHeight-navHeight)];
//
//        UIWindow *window = [UIApplication sharedApplication].keyWindow;
//        [window addSubview:_maskView];
//    }
//}

//-(void)hiddenSearchMaskView{
//    [self.searchBar resignFirstResponder];
//    _maskView.hidden = YES;
//}



#pragma mark ----UISearchBarDelegate----
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
//    _searchBar.frame = CGRectMake(0, 4, self.frame.size.width-40, 30);
    return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self setRightTitle];
//    [self showSearchMaskView];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    
    searchBar.text = nil;
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showPushMessageTitle:)]) {
        [self.delegate showPushMessageTitle:searchBar.text];
    }
    NSLog(@"searchSearchClicked");
    [self appDelegateSearch:searchBar.text];
    
}


-(void)SearchNotification:(NSNotification *)obj{
    NSString *str = [obj userInfo][@"searchName"];
    self.searchBar.text = str;
//    [self appDelegateSearch:str];
}



-(void)appDelegateSearch:(NSString *)str{


   NSMutableArray *array = [ACEPublicTool readDataFromPlist:@"searchHistory" andWillBackType:@"NSArray"];
    if (array == nil) {
        array = [NSMutableArray arrayWithObjects:str, nil];
    }else{
        for (int i = 0; i<array.count; i++) {
            if ([array[i] isEqualToString:str]) {
                break;
            }else{
                if (i == array.count-1) {
                    [array addObject:str];
                }
            }
        }
    }
    
    [ACEPublicTool writeDataToPlist:array andPlistName:@"searchHistory"];
    array = nil;
    
//    [self hiddenSearchMaskView];
//    NPPublicCommodityViewController *commodity = [[NPPublicCommodityViewController alloc]init];
//    commodity.hidesBottomBarWhenPushed = YES;
//
//    commodity.titleName = FILTER_STRING(str);
//    commodity.cookie = COOKIE_COMMODITY_SEARCH;
//    
//    BFUserEntry *userEntry = [BFUserEntry shareInstance];
//    
//    commodity.param =  @{@"userEntry":userEntry.jsonUserEntry,
//                         @"categoryId":@"",
//                         @"commodityName":str,
//                         @"newProduct":@"",
//                         @"offset":@"1",
//                         @"pageSize":@"15"};
//    [self.controller.navigationController pushViewController:commodity animated:YES];
    
//    [_maskView.hotButtonView reloadHistory];
    
//    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [app globalSearchObj:str andController:_controller];
//    [_searchBar resignFirstResponder];

}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}


@end
